package com.h2cloud.aop;

/**
 * @Package: com.h2cloud.aop
 * @ClassName: UserProxyHandler
 * @Author: wulongkai
 * @CreateTime: 2023/9/26 17:47
 * @Description:
 */
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Date;

public class UserProxyHandler implements InvocationHandler {
    private final Object target;

    public UserProxyHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 记录方法调用的时间
        long startTime = System.currentTimeMillis();
        System.out.println("Method " + method.getName() + " started at " + new Date());

        // 调用实际的方法
        Object result = method.invoke(target, args);

        // 记录方法调用的时间
        long endTime = System.currentTimeMillis();
        System.out.println("Method " + method.getName() + " finished at " + new Date());
        System.out.println("Method " + method.getName() + " execution time: " + (endTime - startTime) + " ms");

        return result;
    }
}

